package com.soccertgc.core.game.requests
{
	import com.soccertgc.core.game.GameActionCard;

	public class GameRequestDiscardParams extends GameRequestParams
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON properties names
		protected static const		JSON_PROPERTY_DISCARD_LIST:String		= "discardList";		
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _cards:Vector.<GameActionCard>;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function GameRequestDiscardParams()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------

		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		protected function getCardIndex(card:GameActionCard):int
		{
			for (var i:int = 0; i < _cards.length; i++)
			{
				if (_cards[i].equals(card)) return i;
			}
			return -1;
		}
		
		
		override protected function createActionData():Object
		{
			// Encode all played cards to JSON
			var cards:Array = new Array();
			for (var i:int = 0; i < _cards.length; i++)
			{
				cards.push(_cards[i].instanceId);
			}
			// Encode base data to JSON
			var json:Object = new Object();
			json[JSON_PROPERTY_DISCARD_LIST] = cards;
			// Encoded
			return json;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @param card
		 * @param target
		 * 
		 */		
		public function add(card:GameActionCard):void
		{
			_cards.push(card);
		}
		
		/**
		 * 
		 * @param card
		 * 
		 */		
		public function remove(card:GameActionCard):void
		{
			var i:int = getCardIndex(card);
			_cards.splice(i, 1);
		}
		
		/**
		 * 
		 */		
		override public function clear():void
		{
			_cards.splice(0, _cards.length);
		}
		
	}
}